/**
 * Created by wang on 2016-8-31.
 */
define(function () {
    'use strict';

    function ctrl($scope, $sce, $ionicHistory, InfoService, LocalCacheService, ConcernService, CommonService) {

        $scope.key = "SearchCtrl_search";
        $scope.vm = {};

        init();


        $scope.operUserCare = function(prod_code){
            ConcernService.concern(prod_code);
        }

        $scope.search = function(){

            if(!$scope.search.conditions || CommonService.trim($scope.search.conditions).length == 0) {
                $scope.hasSearch = false;
                return;
            }

            $scope.searchResult = new Array();
            $scope.page_no = 1;
            searchPage();
        }

        $scope.loadMore = function() {
            searchPage();
        }

        $scope.cleanSearch = function(){
            LocalCacheService.remove($scope.key);
            $scope.historySearch = new Array();
        }

        $scope.addHistorySearch = function(fund){
            var historySearch = LocalCacheService.get($scope.key);
            if(!historySearch || historySearch.length <= 0) {
                historySearch = new Array();
            }
            if(historySearch.length == 10) {
                historySearch.shift();
            }
            historySearch.push(fund);
            LocalCacheService.set($scope.key, historySearch);
        }

        $scope.onBackKeyDown = function(){
            $ionicHistory.goBack();
        }

        function init(){
            $scope.hasSearch = false;
            $scope.historySearch = LocalCacheService.get($scope.key);
            $scope.vm.loadMore = false;
            if($scope.historySearch && $scope.historySearch.length > 0) {
                for(var i = 0; i < $scope.historySearch.length; i++) {
                    var h = $scope.historySearch[i];
                    completeFundInfo(h);
                }
            } else {
                $scope.historySearch = new Array();
            }
        }

        function searchPage() {
            InfoService.searchProds($scope.search.conditions, $scope.page_no).then(function(data){
                $scope.hasSearch = true;
                if(data && data.length > 0) {
                    for(var i = 0; i < data.length; i++) {
                        var h = data[i];
                        completeFundInfo(h);
                        h.prod_name_show = h.prod_name.replace($scope.search.conditions, "<mark>"+$scope.search.conditions+"</mark>") +"<em>" +
                        $sce.trustAsHtml(h.prod_code.replace($scope.search.conditions, "<mark>"+$scope.search.conditions+"</mark>")
                        + "&nbsp;" + h.prod_catalog_name) +"</em>";

                    }
                    $scope.searchResult = $scope.searchResult.concat(data);
                    $scope.page_no++;
                } else {
                    $scope.vm.loadMore = false;
                }
                $scope.$broadcast('scroll.infiniteScrollComplete');
            });
        }


        function completeFundInfo(fund){
            fund.isConcern = ConcernService.isConcern(fund.prod_code);
            fund.href = CommonService.productPageUrl(fund.prod_profit_mode, fund.prod_source, fund.prod_catalog_mark);
        }

    }

    ctrl.$inject = ['$scope', '$sce', '$ionicHistory', 'InfoService', 'LocalCacheService', 'ConcernService', 'CommonService'];
    return ctrl;

});